package com.nbicc.authentication.controller;

import com.nbicc.authentication.service.IAuthenticationService;
import com.nbicc.authentication.vo.User;
import com.nbicc.common.ResponseObj;
import com.nbicc.common.utils.BindingResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;

@Api(value = "认证" , description = "登录等")
@RestController
@RequestMapping(value = "/")
public class AuthenticationController {

    @Resource(name = "AuthenticationServiceImpl")
    private IAuthenticationService authenticationService;

    @ApiOperation(value = "登录" , notes = "正确返回200")
    @PostMapping("/login")
    public ResponseObj login(@Valid@RequestBody User user , BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new Exception(BindingResultUtils.getMessages(bindingResult) , new Throwable("非法输入"));
        }
        authenticationService.authentication(user);
        return ResponseObj.success();
    }

    @ApiOperation(value = "登出" , notes = "退出当前登录用户")
    @PutMapping("/logout")
    public ResponseObj logout() throws Exception {
        authenticationService.logout();
        return ResponseObj.success();
    }

}
